﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class AddMianShiEmpWindow : ChildWindow
    {
        public int Year;
        private int ID;
        private int Count;
        private int ZL_ZhiWeiShiJianAnPaiID1;
        private JinShengService.JinShengServiceClient _Client = new JinShengService.JinShengServiceClient();
        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();
        private ZhaoLuService.ZhaoLuServiceClient Client1 =
            new ZhaoLuService.ZhaoLuServiceClient();
        public AddMianShiEmpWindow(int year, int id, int count, int ZL_ZhiWeiShiJianAnPaiid)
        {
            InitializeComponent();
            Year = year;
            ID = id;
            Count = count;
            ZL_ZhiWeiShiJianAnPaiID1 = ZL_ZhiWeiShiJianAnPaiid;
            _Client.GetEmpBasicInfoAccordingToMianShiCompleted += 
                new EventHandler<JinShengService.GetEmpBasicInfoAccordingToMianShiCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToMianShiCompleted);
            _Client.GetEmpBasicInfoAccordingToMianShiAsync(Year, ID, "");
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
            Client1.InsertMianShiEmpCompleted += new EventHandler<ZhaoLuService.InsertMianShiEmpCompletedEventArgs>(Client1_InsertMianShiEmpCompleted);
        }

        void Client1_InsertMianShiEmpCompleted(object sender, ZhaoLuService.InsertMianShiEmpCompletedEventArgs e)
        {
            MessageBox.Show("匹配成功！");
            this.DialogResult = true;
        }

        void _Client_GetEmpBasicInfoAccordingToMianShiCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToMianShiCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems.Count > 0)
            {
                if (SelectItems.Count > Count)
                {
                    MessageBox.Show("所选人员数量多于本职位面试参加的人员数量！");
                    return;
                }
                if (MessageBox.Show("确定向本职位添加所选面试人员吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ObservableCollection<ZL_MianShiEmpPiPeiModel> models =
                        new ObservableCollection<ZL_MianShiEmpPiPeiModel>();
                    foreach(JinShengService.Emp_BasicInfoModel i in SelectItems)
                    {
                        ZL_MianShiEmpPiPeiModel model = new ZL_MianShiEmpPiPeiModel()
                                                            {
                                                                ZhaoLu_DaoRuKaoShengID = Convert.ToInt32(i.OrgID),
                                                                ZhaoLuZhiWeiID = Convert.ToInt32(i.ID),
                                                                ZL_ZhiWeiShiJianAnPaiID = ZL_ZhiWeiShiJianAnPaiID1

                                                            };
                        models.Add(model);
                    }
                    Client1.InsertMianShiEmpAsync(models);

                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要参加面试的人员！");

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                nameOrCard = tbCondition.Text;
                _Client.GetEmpBasicInfoAccordingToMianShiAsync(Year, ID, nameOrCard);
            }
            else
            {
                _Client.GetEmpBasicInfoAccordingToMianShiAsync(Year, ID,"");
            }
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }
    }
}
